/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jellyfishumbrella.GL;

import java.util.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import java.awt.geom.*;
import org.lwjgl.util.vector.*;
import java.util.concurrent.*;
import java.nio.*;
import org.lwjgl.BufferUtils;

/**
 * 
 * @author 500441547
 */
public class Lighting {
	private static FloatBuffer	tempBuffer	= BufferUtils.createFloatBuffer(4);
	private static Vector3f		_pos;

	public Lighting() {

	}

	public static void addAmbient(float[] color) {
		loadTempBuffer(color);
		glLightModel(GL_LIGHT_MODEL_AMBIENT, tempBuffer);
	}

	public static void addDistant(Vector3f orientation, float[] ambColor, float[] diffColor, float[] specColor) {
		addGenericLight(orientation, ambColor, diffColor, specColor);
		_pos = orientation;
	}

	public static void addPoint(Vector3f position, float[] ambColor, float[] diffColor, float[] specColor) {
		addGenericLight(position, ambColor, diffColor, specColor);
		_pos = position;
	}

	private static void addGenericLight(Vector3f position, float[] ambColor, float[] diffColor, float[] specColor) {
		loadTempBuffer(ambColor);
		glLight(GL_LIGHT0, GL_AMBIENT, tempBuffer);
		loadTempBuffer(diffColor);
		glLight(GL_LIGHT0, GL_DIFFUSE, tempBuffer);
		loadTempBuffer(specColor);
		glLight(GL_LIGHT0, GL_SPECULAR, tempBuffer);
		loadTempBuffer(new float[] { position.x, position.y, position.z, 1.0f });
		glLight(GL_LIGHT0, GL_POSITION, tempBuffer);
		glEnable(GL_LIGHT0);
	}

	private static void loadTempBuffer(float[] data) {
		tempBuffer.rewind();
		tempBuffer.put(data);
		tempBuffer.flip();
	}

	public static void render() {
		loadTempBuffer(new float[] { _pos.x, _pos.y, _pos.z, 1.0f });
		glLight(GL_LIGHT0, GL_POSITION, tempBuffer);
	}
}
